{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": false,
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "<table>\n",
    " <tr align=left><td><img align=left src=\"./images/CC-BY.png\">\n",
    " <td>Text provided under a Creative Commons Attribution license, CC-BY. All code is made available under the FSF-approved MIT license. (c) Kyle T. Mandli</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": false,
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "\n",
    "%matplotlib inline\n",
    "import numpy\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "Note to lecturers:  This notebook is designed to work best as a classic Jupyter Notebook with nbextensions \n",
    "* hide_input: to hide selected python cells particularly for just plotting\n",
    "* RISE:  Interactive js slide presentations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# Introduction to Numerical Methods\n",
    "\n",
    "## Outline\n",
    "\n",
    "* Introduction and Motivation\n",
    "* Some examples of Numerical Problems\n",
    "* Overview of Course materials\n",
    "* Course Logistics\n",
    "* <font color=red>**Prerequisites and \"Fluency\"**</font>\n",
    "* Overview of the course computational environment (Python, jupyter, git)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## What are Numerical Methods?\n",
    "\n",
    "* An extremely broad field -- much broader than this class"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* Rough <font color=red>definition</font>:  analysis and application of algorithms to allow computers to solve problems in math, science, engineering..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* Strictly speaking, numerical methods don't require computers (e.g. Newton's method is ~ 17 century)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* But computers make things practical...Consider this class an introduction to computational math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": false,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Why do we need numerical methods?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "### Some problems have <font color=red>**no closed form solution** </font>\n",
    "    \n",
    "   Example:  find $f(x)=0$ for the 5th order polynomial\n",
    "    \n",
    "$$\n",
    "    x^5 + 3x^2+ 2x + 3 = 0\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "$$\n",
    "    p(x) = x^5 + 3x^2+ 2x + 3 = 0\n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "p = lambda x:  x**5 +3*x**2 +2*x +3\n",
    "\n",
    "from scipy.optimize import brentq\n",
    "\n",
    "x = numpy.linspace(-2,2,100)\n",
    "x0 = brentq(p,-2,2)\n",
    "\n",
    "plt.figure(figsize=(8,6))\n",
    "plt.plot(x,p(x),linewidth=2)\n",
    "plt.plot(x,numpy.zeros(x.shape),'k',linewidth=2)\n",
    "plt.scatter(x0,p(x0),s=50,c='r')\n",
    "plt.grid()\n",
    "plt.xlabel('x',fontsize=16)\n",
    "plt.ylabel('p(x)',fontsize=16)\n",
    "plt.title('Root at x={}'.format(x0),fontsize=18)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "The 5th order polynomial  $p(x) = x^5 + 3x^2+ 2x + 3 = 0$ actually has 5 complex roots"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "c = [1,0,0,3,2,3]\n",
    "roots = numpy.roots(c)\n",
    "fig = plt.figure(figsize=(8,6))\n",
    "plt.scatter(numpy.real(roots),numpy.imag(roots),s=50,c='r')\n",
    "x=numpy.linspace(-2,2)\n",
    "plt.plot(x,numpy.zeros(x.shape),'k',linewidth=2)\n",
    "plt.plot(numpy.zeros(x.shape),x,'k',linewidth=2)\n",
    "\n",
    "plt.xlabel('Re',fontsize=16)\n",
    "plt.ylabel('Im',fontsize=16)\n",
    "plt.grid()\n",
    "plt.title('Complex roots of a 5th order polynomial')\n",
    "plt.axis('square')\n",
    "plt.xlim([-2,2])\n",
    "plt.ylim([-2,2])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "which require completely different numerical methods to find."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "In general, the roots of most nonlinear systems,\n",
    "    \n",
    "$$\n",
    "    \\mathbf{F}(x,y,z,t) = \\mathbf{0},\n",
    "$$\n",
    "    \n",
    "cannot be solved directly."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "\n",
    "### Some problems are too big to be done by hand. \n",
    "\n",
    "It may not possible to calculate an exact answer in a convenient amount of time. It may be that a system is over constrained and obtaining an answer that matches all requirements is not possible.\n",
    "\n",
    "<table style=\"width:50%\">\n",
    "  <tr>\n",
    "     <img src=\"./images/linear_regression.png\" width=\"900\"/>\n",
    "  </tr>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Some problems are simply not analytic\n",
    "\n",
    "#### Data analysis of digital data\n",
    "\n",
    "Finding trends in real data represented without a closed form (analytical form).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "data = numpy.loadtxt(\"./data/sunspot.dat\")\n",
    "\n",
    "fig = plt.figure(figsize=(10, 5))\n",
    "fig.set_figwidth(fig.get_figwidth() * 2)\n",
    "\n",
    "axes = fig.add_subplot(1, 2, 1)\n",
    "axes.plot(data[:, 0], data[:, 1],linewidth=2)\n",
    "axes.set_xlabel(\"Year\",fontsize=16)\n",
    "axes.set_ylabel(\"Number\",fontsize=16)\n",
    "axes.set_title(\"Number of Sunspots\",fontsize=18)\n",
    "\n",
    "axes = fig.add_subplot(1, 2, 2)\n",
    "N = int(data.shape[0] / 2)\n",
    "period = 1.0 / numpy.fft.fftfreq(data.shape[0])[1:N]\n",
    "sunspot_freq = numpy.fft.fft(data[:, 1])[1:N]\n",
    "freq = numpy.fft.fftfreq(data.shape[0])[1:N]\n",
    "axes.plot(period, numpy.abs(sunspot_freq)**2,linewidth=2)\n",
    "axes.set_xlabel(\"Years/Cycle\",fontsize=16)\n",
    "axes.set_ylabel(\"Power Spectrum\",fontsize=16)\n",
    "axes.set_title(\"Frequency of Sunspots\",fontsize=18)\n",
    "axes.set_xlim((0, 50))\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Sometimes you actually want an answer \n",
    "\n",
    "(rather than show it exists or is unique)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "##### Example:  Solution of non-linear dynamical systems\n",
    "\n",
    "$$\n",
    "    \\frac{d\\mathbf{u}}{dt} = \\mathbf{F}(t,\\mathbf{u}),\\quad \\mathbf{u}(0)=\\mathbf{u}_0\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* Can prove that if $\\mathbf{F}$ is sufficiently smooth (Lipshitz Continuous) over some domain that a unique solution exists for some local interval in time (Picard-Lindelhof Theorem)\n",
    "* However, the theorem provides <font color=red>**no**</font> way to  efficiently find a solution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Example: Non-Linear population growth\n",
    "\n",
    "Lotka-Volterra Predator-Prey model: Rabbits v. Foxes\n",
    "\n",
    "The variable $R$ represents the number of a prey animals in a population. The variable $F$ represents the number of a predators in a population. The interactions between the two can be approximated with the system of differential equations\n",
    "\n",
    "$$\n",
    "\\begin{align}\n",
    "    \\frac{d R}{dt} &= R \\cdot (a - bF)\\\\\n",
    "    \\frac{d F}{dt} &= F \\cdot (-c + dR), \n",
    "\\end{align}\n",
    "$$\n",
    "\n",
    "where $a,b,c,d$ are parameters of the problem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "# The Lotka Volterra predator prey problem using SciPy's \n",
    "#  ODE integrator solve_ivp\n",
    "\n",
    "from scipy.integrate import solve_ivp\n",
    "\n",
    "def lotkavolterra(t, u, a, b, c, d):\n",
    "    r,f = u\n",
    "    return [r*(a - b*f), f*(-c + d*r) ]\n",
    "\n",
    "a,b,c,d = (1.5, 1, 3, 1)\n",
    "f = lambda t,u : lotkavolterra(t,u,a,b,c,d)\n",
    "\n",
    "time_span = [0, 15]\n",
    "u_0 = [10, 5]\n",
    "sol = solve_ivp(f, time_span , u_0, rtol=1.e-6, atol=1.e-9,dense_output = True)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Numerical Solutions \n",
    "\n",
    "$$\n",
    "    R_0 = 10,\\, F_0 = 5\\quad  a,b,c,d = (1.5, 1, 3, 1)\n",
    "$$\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "t = numpy.linspace(0, 15, 300)\n",
    "z = sol.sol(t)\n",
    "\n",
    "fig = plt.figure(figsize=(16,6))\n",
    "axes = fig.add_subplot(1,2,1)\n",
    "axes.plot(t,z[0],'r',label='rabbits')\n",
    "axes.plot(t,z[1],'b',label='foxes')\n",
    "axes.legend(loc='best',shadow=True)\n",
    "axes.set_xlabel('Time',fontsize=16)\n",
    "axes.set_ylabel('Population',fontsize=16)\n",
    "axes.grid()\n",
    "axes.set_title('Lotka-Volterra Predator Prey system',fontsize=18)\n",
    "\n",
    "axes = fig.add_subplot(1,2,2)\n",
    "axes.plot(z[0],z[1],linewidth=1)\n",
    "axes.grid()\n",
    "axes.set_xlabel('Rabbits',fontsize=16)\n",
    "axes.set_ylabel('Foxes',fontsize=16)\n",
    "axes.set_title('Lotka-Volterra phase portrait',fontsize=18)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Questions an ecologist or a mathematician might ask:\n",
    " - Where are the steady states?\n",
    " - Are the solutions to the system stable?\n",
    " - How do we solve the initial value problem?\n",
    " - How do we understand the non-linear dynamics?\n",
    " - How do we evaluate whether this is a good model?\n",
    " - What impacts do small changes to the different parameters have?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Big Points\n",
    "\n",
    "* Numerics <font color=red>**complement** </font> analytic methods, but don't replace them\n",
    "    \n",
    "* You need both to <font color=red> **understand** </font> your problems\n",
    "\n",
    "* Numerical methods are only a part of the bigger problem of <font color=red>**Mathematical Modeling**</font>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": true,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "## Mathematical Modeling\n",
    "\n",
    "<table style=width:100%>\n",
    "    <tr>\n",
    "        <th></th>\n",
    "        <th><img src=\"./images/mathModeling.png\" width=\"800\"/><th>\n",
    "    </tr>\n",
    "</table>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* Murphy was a modeler..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "* Strictly speaking this is not a course in Mathematical Modeling"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "We usually have experience and insights into a system and have a good idea what the solutions should be. At the same time the numerics can help us build our intuition and complement analytical methods. The numerics can also be used to test ideas and extend our understanding of a system. In doing so we can update mathematical descriptions to account for new aspects.\n",
    "\n",
    "The process of describing physical systems using mathematical expressions is often referred to as mathematical modeling. It is a process in which intuition, simulation, and analysis are used to build expressions that mimic general behaviours of physical systems. The general practice is to start with a basic description and refine it over many iterations. \n",
    "\n",
    "Each step in the process introduces error. In this course we will focus on the errors associated with numerical approximation. It is important, though, to be able to determine the difference between errors associated with simplifications associated with the model and the errors associated with the approximation of mathematical expressions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": true,
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "## Why is this exciting?\n",
    "\n",
    "Computers have revolutionized our ability to explore...\n",
    "\n",
    "[Top 10 Algorithms of the 20th Century](http://www.siam.org/pdf/news/637.pdf?t=1&cn=ZmxleGlibGVfcmVjcw%3D%3D&refsrc=email&iid=658bdab6af614c83a8df1f8e02035eae&uid=755271476&nid=244+285282312)\n",
    "\n",
    "...and there is more to come!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## The issues:\n",
    "### Accuracy and Efficiency\n",
    "\n",
    "- Numerical methods, invariably include an enormous range of <font color=red> approximations </font>, each with attendant <font color=red> errors </font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- Good numerical methods also return error estimates, and are stable in the presence of floating point error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "source": [
    "- The detailed analysis of algorithms and their errors is formally <font color=red> Numerical Analysis </font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## <font color=red> **This is not a class in numerical analysis** </font>\n",
    "\n",
    "This is principally a Methods class where I will emphasize\n",
    "\n",
    "- Standard Methods and their errors\n",
    "- Give insight into how they work (and when they don't)\n",
    "- Give you practice implementing them to solve **your** problems\n",
    "- Point you to high quality numerical software that you should be using in your own work\n",
    "- <font color=red> Help you make good choices <font>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": true,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "\n",
    "<table style=width:100%>\n",
    "    <tr>\n",
    "<img src=\"./images/MakeGoodChoices.gif\" width=\"800\"/>\n",
    "    </tr>\n",
    "</table>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": true,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "\n",
    "<table style=width:100%>\n",
    "    <tr>\n",
    "<img src=\"./images/RuiningMyLife.gif\" width=\"800\"/>\n",
    "    </tr>\n",
    "</table>\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Course Content\n",
    "\n",
    "Topics Covered: (see Syllabus on [Courseworks](https://courseworks2.columbia.edu/courses/95683/assignments/syllabus))\n",
    "\n",
    "1. Quick <font color=red>*Review*</font> of computational Tools (Python,numpy,matplotlib,scipy,git)\n",
    "- Sources of Error and Error Analysis\n",
    "- Rootfinding/Optimization of non-linear functions $f(x)$ \n",
    "- Interpolation\n",
    "- Numerical integration (quadrature) and differentiation\n",
    "- Solution of ODE Initial value problems\n",
    "- Numerical Linear Algebra\n",
    "- Solving <font color=red> systems </font> of non-linear equations $\\mathbf{F}(\\mathbf{x})=\\mathbf{0}$\n",
    "- ODE 2-point Boundary value problems (towards numerical PDE's)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Topics Not Covered: \n",
    "\n",
    "1. Optimization -- linear programming, constrained optimization\n",
    "- Numerical Solution of PDE's (APMA E4301)\n",
    "- Mathematical Modeling (maybe a little)\n",
    "- Machine Learning/Data Science (APMA 4990 Intro Math of Data Science)\n",
    "- Lot's of things that would be fun...\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Purpose of this course:\n",
    "\n",
    "Choose and understand critical methods that are essential for Numerical PDE's, modeling and scientific computation.\n",
    "\n",
    "\n",
    "<table style=\"width:100%\">\n",
    "  <tr>\n",
    "    <th><img src=\"./images/siemens-volrender0000_kirby_modeling.png\" width=\"500\"/></th>\n",
    "    <th><img src=\"./images/Fenics_tc_vm.png\" width=\"500\"/></th> \n",
    "  </tr>\n",
    "    <td align=center>HPC model of Windturbine turbulence by <a href=http://students.uwyo.edu/akirby/>Andrew Kirby</a></th>\n",
    "    <td align=center><a href=http://fenicsproject.org>FEniCS</a> FEM model of a turbocharger</th>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": true,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### A FEM model from Spiegelman\n",
    "\n",
    "Coupled fluid/solid dynamics model of magmatism in the Earth's mantle."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hide_input": true,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "from IPython.lib.display import YouTubeVideo\n",
    "vid = YouTubeVideo('Pfp4snvqhus',rel=0,width=700,height=450, loop=1)\n",
    "display(vid)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### All of these problems require <font color=red>Interpolation, numerical quadrature,numerical linear algebra </font>,\n",
    "\n",
    "#### Plus PDE's, Vector Calculus, functional analysis,computational geometry, C++/Python programming"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hide_input": true,
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Course Logistics\n",
    "\n",
    "- 2 Lectures per week\n",
    "- ~1 Homework per week (All jupyter notebook based) (50%)\n",
    "- 1 takehome Midterm (25%)\n",
    "- 1 takehome Final (25%)\n",
    "\n",
    "\n",
    "- No primary text but\n",
    "- All notes available as Jupyter notebooks through github"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Course Pre-requisites\n",
    "\n",
    "### Numerical methods aren't particularly hard...\n",
    "\n",
    "* however...They require significant <font color=red> **fluency** </font> in\n",
    "    * Calculus\n",
    "    * Vector Calculus\n",
    "    * ODE's\n",
    "    * Linear Algebra\n",
    "    * Programming: Python 3 (numpy, matplotlib, scipy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Computational Fluency \n",
    "- the ability to:\n",
    "    - move smoothly between continous and discrete problems\n",
    "    - recognize and use a range of mathematical/computational techniques\n",
    "    - use best practices for software development \n",
    "    - keep track of errors and artifacts\n",
    "    - stay on top of complex, multi-part problems\n",
    "    - <font color=red>**Debug**</font>\n",
    "    \n",
    "- Anything that can go wrong...will go wrong"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Computational Tools/Environment"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Computer Languages\n",
    "\n",
    "* Compiled languages: C, C++, Fortran\n",
    "* Commercial interactive modeling systems: Matlab, Mathematical, IDL\n",
    "* Open-source interpreted languages: Python, R, Ruby, Julia"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "#### C, C++, Fortran\n",
    "\n",
    "##### Pros:\n",
    " - Performance and legacy computing codes available\n",
    "\n",
    "##### Cons:\n",
    " - Syntax not optimized for casual programming\n",
    " - No interactive facilities\n",
    " - Difficult visualization, text processing, etc.\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "#### IDL, Matlab, Mathematica, etc.\n",
    "\n",
    "##### Pros:\n",
    " - Interactive with easy visualization tools\n",
    " - Extensive scientific and engineering libraries available\n",
    "\n",
    "##### Cons:\n",
    " - Costly and proprietary\n",
    " - Unpleasant for large-scale computing and non-mathematical tasks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Python (3.x)\n",
    "\n",
    "##### Features and Project Goals:\n",
    " - Python is free (BSD-like license) and highly portable (Windows, Mac OS X, Linux, etc.)\n",
    " - Interactive interpreter\n",
    " - Readability\n",
    " - Simple\n",
    " - Extensive documentation\n",
    " - Memory management is (mostly) transparent\n",
    " - Clean and object-oriented\n",
    " - Built-in types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "##### Pros:\n",
    "- Comprehensive standard library\n",
    " - Well-established 3rd-party packages (NumPy, SciPy, matplotlib, etc.)\n",
    " - Easily wraps existing legacy code in C, C++ and Fortran\n",
    " - Python mastery is marketable\n",
    " - Scalability\n",
    "   - Interactive experimentation\n",
    "   - Good tools for documenting code, and the resulting code tends to be easier to maintain compared to other interpreted languages.\n",
    "   - Code can be one-line scripts or million-line projects\n",
    "   - Used by novices and full-time professionals alike"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "##### Cons:\n",
    " - Can be slow\n",
    " - Packaging system is a bit crufty\n",
    " - Discipline is forced on the programmer by the design of the language. Can limit choices available for how to implement an algorithm. (The *python* way.)\n",
    " - Too many Monty Python jokes (not really a con)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Version Control via `git`\n",
    "\n",
    "`git` is version control system allowing a user to take \"snapshots\" of data.  These snapshots can then be compared, combined or recalled as needed.\n",
    "\n",
    "Why version control (or `git`)?\n",
    " - Nearly ubiquitous in modern software engineering and hence an essential skill\n",
    " - Can save you from yourself\n",
    " - Greatly simplifies the way multiple people can work on and share a single project.\n",
    " - Steep learning curve so expose yourself often for maximal success\n",
    " \n",
    "[Learn more!](https://git-scm.com/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Jupyter Notebooks\n",
    "\n",
    "The notebook environment gives us a convenient means for working with code while annotating it.  We will only cover the key concepts here and hope that you will explore on your own the environments.\n",
    "\n",
    "[Documentation](https://jupyter.readthedocs.io/en/latest/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Toolbar\n",
    "\n",
    " - Notebooks are modal, they have an edit mode (editing cells) and command mode.\n",
    " - Highly recommend taking the tour and checking out the help menu "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Content types\n",
    "   - Markdown\n",
    "   - LaTeX -- $x^2 + y^2 = \\sin(x)$\n",
    "   - Python\n",
    "   - NumPy, SciPy, and other packages\n",
    "   - Other languages (R, Julia)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Running the Notebooks\n",
    "\n",
    "To run and interact with the notebooks you need the Jupyter software and scipy software stack\n",
    "\n",
    "#### Interactive Environments -- all web-based\n",
    "\n",
    "* Jupyter Lab\n",
    "* Classical Jupyter Notebook\n",
    "* Presentation modes (e.g. RISE)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### Standard Scipy software stack\n",
    "* Python 3.x\n",
    "* SciPy\n",
    "* Numpy\n",
    "* Matplotlib\n",
    "* Sympy\n",
    "* Pandas\n",
    "* etc "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "### Installations: A few options\n",
    "\n",
    "#### Install on your <font color=red>own machine </font>\n",
    " - [Notebook Quick Start Guide](https://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/index.html)\n",
    " - [Anaconda](http://anaconda.com/downloads)\n",
    " - [Canopy](https://store.enthought.com/downloads/) \n",
    "\n",
    "Remember to grab the <font color=red> **Python 3.x** </font> versions!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "#### The \"cloud\"\n",
    "\n",
    "Instead of running things locally on your machine there are a number of cloud services that you are welcome to use in order to get everything running easily:\n",
    " - [<font color=red>Columbia's CUIT Jupyter Hub server</font>](https://apma4300.columbiajupyter2.org/)\n",
    " - [Google's Colaboratory](https://colab.research.google.com)\n",
    " - [Microsoft Azure Notebooks](https://notebooks.azure.com)\n",
    " - [CoCalc](https://cocalc.com/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### Obtaining the Notebooks\n",
    "\n",
    "All notebooks are found on [github](http://github.com/mandli/intro-numerical-methods).\n",
    "\n",
    "Highly recommend obtaining a github account if you do not already have one.  Will allow you start to become comfortable with `git`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "**Clone** the repository\n",
    "\n",
    "`$> git clone https://github.com/mspieg/intro-numerical-methods.git`\n",
    "\n",
    "**Pull** in new changes\n",
    "\n",
    "`$> git pull`\n",
    "\n",
    "**Push** new changes (you do not have permission to do this\n",
    "\n",
    "`$> git push`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "Also note that you can watch what changes were made and submit **issues** to the github project page if you find mistakes (PLEASE DO THIS!). It will be much easier to do so if you have your own account on github and work with your own clone. You can then submit a merge request to have your changes incorporated into other repositories."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "source": [
    "### BRING YOUR LAPTOP!\n",
    "\n",
    "In class demos are better with your participation!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "skip"
    }
   },
   "source": [
    "### Peer Review\n",
    "\n",
    "**Why?**\n",
    "In this class reviewing your peer's work can lead to\n",
    " - Better understanding of a problem\n",
    " - See alternative solutions to the same problem\n",
    " - Learn how to read other people's code\n",
    " - Hopefully learn some tips about your own coding style\n",
    " - Another skill highly used in practice\n",
    " - Demonstrate why readibility and discipline with respect to code matters."
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  },
  "latex_envs": {
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 0
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
